load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@darwin_config//:config.bzl", "BUNDLE_ID_PREFIX")
load("@rules_apple//apple:apple.bzl", "apple_static_xcframework", "apple_xcframework")
load("@rules_apple//apple:ios.bzl", "ios_application", "ios_framework")
load("@rules_apple//apple:resources.bzl", "apple_resource_bundle")
load("@rules_apple//apple:versioning.bzl", "apple_bundle_version")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load(
    "@rules_xcodeproj//xcodeproj:defs.bzl",
    "top_level_target",
    "xcodeproj",
)
load(
    "//platform/ios/bazel:files.bzl",
    "MLN_IOS_PRIVATE_HEADERS",
    "MLN_IOS_PUBLIC_HEADERS",
    "MLN_IOS_PUBLIC_OBJCPP_METAL_SOURCE",
    "MLN_IOS_PUBLIC_OBJCPP_OPENGL_SOURCE",
    "MLN_IOS_PUBLIC_OBJCPP_SOURCE",
    "MLN_IOS_PUBLIC_OBJC_SOURCE",
    "MLN_IOS_SDK_HEADERS",
    "MLN_PUBLIC_IOS_APP_SOURCE",
)
load("//platform/ios/bazel:macros.bzl", "info_plist")
load("//platform/ios/bazel:provisioning.bzl", "configure_device_profiles")

configure_device_profiles()

filegroup(
    name = "ios_sdk_hdrs",
    srcs = MLN_IOS_SDK_HEADERS,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "ios_public_hdrs",
    srcs = MLN_IOS_PUBLIC_HEADERS,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "ios_private_hdrs",
    srcs = MLN_IOS_PRIVATE_HEADERS,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "ios_objcpp_srcs",
    srcs = MLN_IOS_PUBLIC_OBJCPP_SOURCE + select({
        "//:metal_renderer": MLN_IOS_PUBLIC_OBJCPP_METAL_SOURCE,
        "//conditions:default": MLN_IOS_PUBLIC_OBJCPP_OPENGL_SOURCE,
    }),
    visibility = ["//visibility:public"],
)

filegroup(
    name = "ios_objc_srcs",
    srcs = MLN_IOS_PUBLIC_OBJC_SOURCE,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "ios_app_srcs",
    srcs = MLN_PUBLIC_IOS_APP_SOURCE,
    visibility = ["//visibility:public"],
)

info_plist(
    name = "info_plist",
    out = "Info.plist",
    base_info_plist = "framework/Info.plist",
)

info_plist(
    name = "info_static_plist",
    out = "Info-static.plist",
    base_info_plist = "framework/Info-static.plist",
)

info_plist(
    name = "info_app_plist",
    out = "Info-app.plist",
    base_info_plist = "app/Info.plist",
)

genrule(
    name = "mln_defines",
    outs = ["MLNDefines.h"],
    cmd = select({
        "//:metal_renderer": "echo '#define MLN_RENDER_BACKEND_METAL 1' > $@",
        "//conditions:default": "echo '' > $@",
    }),
)

public_hdrs = [
    ":mln_defines",
    ":ios_public_hdrs",
    ":ios_sdk_hdrs",
    "//platform/darwin:darwin_objc_hdrs",
    "//platform/darwin:generated_style_public_hdrs",
]

apple_static_xcframework(
    name = "MapLibre.static",
    bundle_id = "com.maplibre.mapbox",
    bundle_name = "MapLibre",
    infoplists = ["info_static_plist"],
    ios = {
        "simulator": [
            "x86_64",
            "arm64",
        ],
        "device": ["arm64"],
    },
    minimum_os_versions = {"ios": "12.0"},
    public_hdrs = public_hdrs,
    version = ":maplibre_ios_version",
    visibility = ["//visibility:public"],
    deps = ["//platform:ios-sdk"],
)

apple_bundle_version(
    name = "maplibre_ios_version",
    build_label_pattern = "maplibre_ios_{version}",
    build_version = "{version}",
    capture_groups = {
        "version": "[0-9]+.[0-9]+.[0-9]+",
    },
    fallback_build_label = "maplibre_ios_1.2.3456789",
    short_version_string = "{version}",
)

apple_xcframework(
    name = "MapLibre.dynamic",
    bundle_id = "com.maplibre.mapbox",
    bundle_name = "MapLibre",
    data = glob([
        "resources/*.lproj/**",
        "resources/*.xcassets/**",
    ]) + [
        "PrivacyInfo.xcprivacy",
        "resources/metal-cpp-ignores.txt",
    ],
    infoplists = ["info_plist"],
    ios = {
        "simulator": [
            "x86_64",
            "arm64",
        ],
        "device": ["arm64"],
    },
    minimum_os_versions = {"ios": "12.0"},
    public_hdrs = public_hdrs,
    version = ":maplibre_ios_version",
    visibility = ["//visibility:public"],
    deps = ["//platform:ios-sdk-dynamic"],
)

ios_framework(
    name = "MapLibre.link",
    testonly = True,
    bundle_id = "com.maplibre.link",
    families = ["iphone"],
    infoplists = ["info_plist"],
    linkopts = [""],
    minimum_os_version = "12.0",
    visibility = ["//visibility:public"],
    deps = ["//platform:ios-sdk"],
)

apple_resource_bundle(
    name = "resources",
    bundle_id = "com.maplibre.mapbox",
    bundle_name = "Mapbox",
    infoplists = ["info_static_plist"],
    resources = glob([
        "resources/*.lproj/**",
        "resources/*.xcassets/**",
        "resources/metal-cpp-ignores.txt",
    ]),
    visibility = ["//visibility:public"],
)

_IOS_APPLICATION_RESOURCES = [
    "app/numeric_filter_style.json",
    "app/missing_icon.json",
    "app/points.geojson",
    "app/polyline.geojson",
    "app/Settings.bundle",
    "app/Main.storyboard",
    "app/threestates.geojson",
    "app/LaunchScreen.storyboard",
    "app/simple_route.json",
    "app/fill_filter_style.json",
    "//platform/darwin:app/PluginLayerTestStyle.json",
    "//platform/darwin:test/amsterdam.geojson",
] + glob([
    "app/*.lproj/**",
    "app/Assets.xcassets/**",
])

apple_bundle_version(
    name = "maplibre_app_version",
    build_version = "1.0.0",
)

ios_application(
    name = "App",
    bundle_id = "{}.maplibre.app".format(BUNDLE_ID_PREFIX),
    families = [
        "iphone",
        "ipad",
    ],
    infoplists = ["info_app_plist"],
    minimum_os_version = "12.0",
    provisioning_profile = "xcode_profile",
    resources = _IOS_APPLICATION_RESOURCES,
    version = ":maplibre_app_version",
    visibility = ["//visibility:public"],
    deps = [
        "//platform:iosapp",
    ],
)

ios_application(
    name = "BenchmarkApp",
    bundle_id = "{}.benchmark.app".format(BUNDLE_ID_PREFIX),
    families = [
        "iphone",
        "ipad",
    ],
    infoplists = ["//platform/ios/benchmark:info_benchmark_plist"],
    minimum_os_version = "12.0",
    provisioning_profile = "xcode_profile",
    resources = [
        "//platform/ios/benchmark:bundle_resources",
        "//platform/ios/benchmark/assets:benchmark_assets",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//platform:ios-benchapp",
        "//platform:ios-sdk",
    ],
)

filegroup(
    name = "extra_files",
    srcs = [
        "BAZEL.md",
        "CHANGELOG.md",
        "MapLibre.podspec",
        "PrivacyInfo.xcprivacy",
        "README.md",
        "RELEASE.md",
        "VERSION",
    ],
    visibility = ["@rules_xcodeproj//xcodeproj:generated"],
)

xcodeproj(
    name = "xcodeproj",
    bazel_env = {"PATH": "/bin:/usr/bin:/usr/local/bin:/opt/homebrew/bin"},
    default_xcode_configuration = "Debug",
    extra_files = [
        ":extra_files",
    ],
    minimum_xcode_version = "14",
    project_name = "MapLibre",
    tags = ["manual"],
    top_level_targets = [
        top_level_target(
            "App",
            target_environments = [
                "simulator",
                "device",
            ],
        ),
        top_level_target(
            "//platform/ios/app-swift:MapLibreApp",
            target_environments = [
                "simulator",
                "device",
            ],
        ),
        top_level_target(
            "BenchmarkApp",
            target_environments = [
                "simulator",
                "device",
            ],
        ),
        top_level_target(
            "//render-test/ios:RenderTest",
            target_environments = [
                "simulator",
                "device",
            ],
        ),
        top_level_target(
            "//platform/ios/iosapp-UITests:uitest",
            target_environments = [
                "simulator",
                "device",
            ],
        ),
        top_level_target(
            "//platform/ios/test:ios_test",
            target_environments = [
                "simulator",
                "device",
            ],
        ),
        top_level_target(
            "//platform/ios/test/core:CppUnitTests",
            target_environments = [
                "simulator",
                "device",
            ],
        ),
    ],
    xcode_configurations = {
        "Debug": {"//command_line_option:compilation_mode": "dbg"},
        "Release": {"//command_line_option:compilation_mode": "opt"},
    },
)

sh_binary(
    name = "lint-plists",
    srcs = ["//platform/ios:scripts/lint-plists.sh"],
    data = [
        "Integration_Test_Harness/Info.plist",
        "Integration_Tests/Info.plist",
        "app/Info.plist",
        "bench_UITests/Info.plist",
        "framework/Info.plist",
        "framework/Info-static.plist",
        "//platform/ios/benchmark:Info.plist",
        "//platform/ios/iosapp-UITests:Info.plist",
        "//platform/ios/test:Info.plist",
    ],
)

exports_files(
    MLN_IOS_PUBLIC_HEADERS +
    MLN_IOS_PRIVATE_HEADERS +
    MLN_IOS_PUBLIC_OBJCPP_SOURCE +
    MLN_IOS_PUBLIC_OBJCPP_METAL_SOURCE +
    MLN_IOS_PUBLIC_OBJCPP_OPENGL_SOURCE +
    MLN_IOS_PUBLIC_OBJC_SOURCE +
    MLN_PUBLIC_IOS_APP_SOURCE,
)

js_library(
    name = "symbols-to-check",
    srcs = [
        ":ios_objc_srcs",
        ":ios_objcpp_srcs",
        ":ios_private_hdrs",
        ":ios_public_hdrs",
        ":ios_sdk_hdrs",
    ],
    visibility = ["//platform:__pkg__"],
)
